﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace 桌面视频录制
{
    public delegate void DrawImageDelegate(Bitmap image, long playLength);
    public delegate void PlayOverDelegate();
    class VideoPlay
    {
        private string filePath;
        private int interval;
        private DrawImageDelegate d;
        private FileStream fs;
        private BinaryReader br;
        private bool isStop;//当前是否为停止操作
        private Thread thread;

        public int Interval
        {
            set { interval = value; }
        }
        private bool isExit;

        public event PlayOverDelegate PlayOver;
        private long totalLength;
        private long playLength;

        public long TotalLength
        {
            get { return totalLength; }
            set { totalLength = value; }
        }
        public VideoPlay(string filePath, int interval, DrawImageDelegate d)
        {
            this.filePath = filePath;
            this.interval = interval;
            this.d = d;
            this.fs = File.OpenRead(filePath);
            this.br = new BinaryReader(fs);
            this.totalLength = br.ReadInt64();
        }
        public void Play()
        {
            this.isExit = false;
            thread = new Thread(StartPlay);
            thread.IsBackground = true;
            thread.Start();
        }
        private void StartPlay(object obj)
        {
            while (isExit == false)
            {
                try
                {
                    int length = br.ReadInt32();
                    byte[] bs = br.ReadBytes(length);
                    playLength++;
                    using (MemoryStream ms = new MemoryStream(bs))
                    {
                        d(new Bitmap(ms), playLength);
                    }
                    Thread.Sleep(interval);
                }
                catch (Exception)
                {
                    if (PlayOver != null && isExit == false)
                        PlayOver();
                    break;
                } 
            }
            if (isStop || !isExit)
            {
                fs.Close();
                br.Close();
            }
        }
        private bool GetPlayPositon(BinaryReader br, FileStream fs)
        {
            try
            {
                fs.Seek(8, SeekOrigin.Begin);
                for (long i = 0; i < playLength; i++)
                {
                    int length = br.ReadInt32();
                    fs.Seek(length, SeekOrigin.Current);
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public void PlayFromSetPos(long newPos)
        {
            this.Pause();
            thread.Abort();
            thread.Join();
            this.playLength = newPos;
            if (GetPlayPositon(br, fs))
                this.Play();
            else
            {
                MessageBox.Show("定位异常!");
                this.Stop();
            }
        }
        public void Pause()
        {
            this.isStop = false;
            this.isExit = true;
        }
        public void Stop()
        {
            this.isStop = true;
            this.isExit = true;
        }
    }
}
